package com.xhu.collegestudentrentalsystem.module.manager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.xhu.collegestudentrentalsystem.module.manager.service.IManagerService;
import com.xhu.collegestudentrentalsystem.module.user.dao.HouseDAO;
import com.xhu.collegestudentrentalsystem.module.user.dao.UserDAO;
import com.xhu.collegestudentrentalsystem.module.user.entity.House;
import com.xhu.collegestudentrentalsystem.module.user.entity.Page;
import com.xhu.collegestudentrentalsystem.module.user.entity.User;
import com.xhu.collegestudentrentalsystem.util.constant.EntityProperty;
import com.xhu.collegestudentrentalsystem.util.constant.StatusMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * @author zhu huilin
 * @date 2023/3/29 15:53
 */
@Service
public class ManagerServiceImpl implements IManagerService {

    @Autowired
    private UserDAO userDAO;

    @Autowired
    private HouseDAO houseDAO;

    /**
     * 查询房东和租客的用户信息
     */
    @Override
    public List<User> findAllUser(Page page) {
        PageHelper.startPage(page.getCurrent(), page.getLimit());
        QueryWrapper<User> qw = new QueryWrapper<User>().ne(EntityProperty.user.USER_ROLE, EntityProperty.userRole.ADMIN);
        List<User> users = userDAO.selectList(qw);
        Integer count = userDAO.selectCount(qw);
        page.setRows(count);
        return users;
    }

    /**
     * 更改用户状态
     */
    @Override
    public Integer updateUserStatus(Long userId) {
        User user = userDAO.selectOne(new QueryWrapper<User>().eq(EntityProperty.ID, userId));
        if(user != null){
            if(user.getStatus() == 1){
                return userDAO.updateUserStatus(userId, StatusMsg.userStatus.LOCK);
            }else if(user.getStatus() == 0) {
                return userDAO.updateUserStatus(userId, StatusMsg.userStatus.NORMAL);
            }
        }
        return 0;
    }

    /**
     * 查询所有房源信息，包含未审核、审核通过的，不包括房东删除的
     */
    @Override
    public List<House> findAllHouses(Page page) {
        PageHelper.startPage(page.getCurrent(), page.getLimit());
        QueryWrapper<House> qw = new QueryWrapper<House>().ne(EntityProperty.STATUS, StatusMsg.houseStatus.OWNER_DELETE);
        List<House> houses = houseDAO.selectList(qw);
        Integer count = houseDAO.selectCount(qw);
        page.setRows(count);
        return houses;
    }

    /**
     * 房子审核通过，只需要修改房子状态即可
     */
    @Override
    public Integer passHouse(Long houseId) {
        return houseDAO.updateHouseStatus(houseId, StatusMsg.houseStatus.UN_RENT);
    }

    /**
     * 房子审核未通过
     */
    @Override
    public Integer unPassHouse(Long houseId) {
        return houseDAO.updateHouseStatus(houseId, StatusMsg.houseStatus.UN_PASS);
    }
}
